package in.muton.edushare.pages.deptpages;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.PageRoleCheck;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.univ.UnivPage;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.summary.html.DeptSummary;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Departments;

public abstract class DeptBase extends EduShareBasePage {

	protected static String[] menuItems = { "deptNews", "folders", "deptCalendar",
			"acadProgs", "DeptBatches", "deptStudents", "deptProfs", "deptAdmins",
			"deptEveryone", "deptCourses", "deptUniv", "deptCP", "deptProfile",
			"deptInvite", "deptCourse", "DeptPoll"};

	public DeptBase(PageParameters params) {
		super();
		add(CSSPackageResource.getHeaderContribution(DeptBase.class,
				"deptHome.css"));
		getDept(params);
		add(new ProfileImage("deptIcon", getModel(), ApplicationConstants.FILE_SUFFIX_ICON));
		add(new Label("deptName", getModel().getObject().getDeptName()));

		Link<Void> l = new BookmarkablePageLink<Void>("univLink",
				UnivPage.class, EntityPageParams.getParams(dd.getOwner()));
		l.add(new Label("univName", dd.getOwner().getObject()
				.getUniversityName()));
		add(l);
		add(new BookmarkablePageLink<Void>("vwProfile", URLLoader.getPageClass(URLConstants.DEPT_PUBLIC_PAGE_HOME), EntityPageParams.getParams(getModel())));
		add(new Link<Void>("printReport"){

			private static final long serialVersionUID = 563340255538035753L;

			@Override
			public void onClick() {
				setResponsePage(new DeptSummary(DeptBase.this.model));
			}});
		setMenuItems(params);
	}

	private void setMenuItems(PageParameters params) {
		String highlight = getActivePage(params);
		DeptRoleCheck drc = new DeptRoleCheck(getModel().getObject(),
				getLoggedInUser());
		for (String s : menuItems) {
			WebMarkupContainer wmc;
			add(wmc = new WebMarkupContainer(s));
			EntityPageParams.getParams(getModel(), s);
			if (highlight.compareTo(s) == 0) {
				wmc.add(new AttributeAppender("class", new Model<String>(
						"es-selected"), " "));
			}
			if (s.compareTo("deptNews") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", DeptPage.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.compareTo("folders") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", DeptFolders.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.compareTo("deptCalendar") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", DeptCalendar.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.compareTo("acadProgs") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", DeptAcadProgs.class,
						EntityPageParams.getParams(getModel())));
			}else if(s.equals("DeptBatches")){ 
				wmc.add(new BookmarkablePageLink<Void>("link", DeptBatches.class,
						EntityPageParams.getParams(getModel())));
			}
			else if (s.compareTo("deptStudents") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						DeptPeople.class, EntityPageParams.getParams(
								getModel(), s)));
			} else if (s.compareTo("deptProfs") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						DeptPeople.class, EntityPageParams.getParams(
								getModel(), s)));
			} else if (s.compareTo("deptAdmins") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						DeptPeople.class, EntityPageParams.getParams(
								getModel(), s)));
			} else if (s.compareTo("deptEveryone") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						DeptPeople.class, EntityPageParams.getParams(
								getModel(), s)));
			} else if (s.compareTo("deptCourses") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						DeptCourses.class, EntityPageParams
								.getParams(getModel())));
			} else if (s.compareTo("deptUniv") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", DeptUniv.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.compareTo("deptCP") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						DeptSetting.class, EntityPageParams
								.getParams(getModel())));
				wmc.setVisible(false);
			} else if (s.compareTo("deptProfile") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", DeptEdit.class,
						EntityPageParams.getParams(getModel())));
				wmc.setVisible(drc.isUnivAdmin() || drc.isPrimaryAdmin()
						|| drc.isSecondaryAdmin());
			} else if (s.compareTo("deptInvite") == 0) {
				if (!DeptRoleCheck.isPrimaryAdmin(getModel().getObject(),
						EduShareSession.get().getLoggedInUser())
						&& !DeptRoleCheck.isUnivAdmin(getModel().getObject(),
								EduShareSession.get().getLoggedInUser()))
					wmc.setVisible(false);

				wmc.add(new BookmarkablePageLink<Void>("link",
						DeptInvite.class, EntityPageParams
								.getParams(getModel())));
			} else if (s.compareTo("deptCourse") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", NewCourse.class,
						EntityPageParams.getParams(getModel())));
				wmc.setVisible(drc.isPrimaryAdmin()||drc.isUnivAdmin());
			} else if (s.equals("DeptPoll")) {
				wmc.add(new BookmarkablePageLink<Void>("link", DeptPolls.class,
						EntityPageParams.getParams(getModel())));
				wmc.setVisible(drc.isUnivAdmin() || drc.isPrimaryAdmin()
						|| drc.isSecondaryAdmin() || drc.isTeacher());
			} 
		}
	}

	protected abstract String getActivePage(PageParameters params);

	private void getDept(PageParameters params) {
		String id = params.getString("1");
		if (StringUtil.isValid(id)) {
			Departments dept = new DepartmentsDAO(getSessionORMInterface())
					.getDepartmentByPk(Integer.parseInt(id));
			if (dept != null) {
				setModel(new CayenneModel<Departments>(dept));
				dd = new DeptDataHandler(getModel());
				prc = new PageRoleCheck(dept, getLoggedInUser());
				return;
			}
		}
		throw new EduShareRuntimeException("Cannot find Department");
	}

	@Override
	public String getUserRole() {
		return " : " + dd.getDeptRole(EduShareSession.get().getLoggedInUser());
	}

	public void setModel(IModel<Departments> model) {
		this.model = model;
	}

	public IModel<Departments> getModel() {
		return model;
	}

	protected DeptDataHandler dd = null;
	private IModel<Departments> model = null;
}
